var express = require('express'),
	db = require('../pubModule/mysql'),
	encode = require('../pubModule/crypto'),
	reg = express();


reg.get('/',function(req, res){
	res.render('reg',{})
})

reg.get('/ajaxUsername',function(req, res){
	var query = 'select * from users where name = "' + req.query.selectName + '"'
	db.poolSelectOne(query, function(err, row){
		if (row == '' || row == null) {
			res.json({'status':true})
		} else {
			res.json({'status':false})
		}
	})
})

reg.post('/', function(req, res){
	try {
		var query = 'insert into users (name,password) values("'+req.body.name+
			'","'+req.body.password+'")'
		db.poolSelectOne(query, function(err, row){
			if (err) {
				res.redirect(301,'/reg')
			} else {
				res.redirect(301,'/login')
			}
		})
	} catch (e) {

	}
	
})

module.exports = reg